package com.gfm.story.service.adver;

import com.gfm.story.dao.AdvertisementsDao;
import com.gfm.story.dao.DictionaryDao;
import com.gfm.story.dao.DictionaryDetailDao;
import com.gfm.story.entity.advertisements.Advertisements;
import com.gfm.story.entity.base.ResultBean;
import com.gfm.story.entity.dict.Dictionary;
import com.gfm.story.entity.dict.DictionaryDetail;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @BelongsProject: book
 * @BelongsPackage: com.gfm.book.service.advertisements
 * @Author: Administrator
 * @CreateTime: 2019-06-14 14:40
 * @Description: ${Description}
 */
@Service
public class AdvertisementsService {

    @Resource
    private AdvertisementsDao advertisementsDao;
    @Resource
    private DictionaryDao dictionaryDao;

    public ResultBean selectAdverAllOpen() {
        Dictionary dictionary = new Dictionary();
        dictionary.setDictCode("AdverDomainAddress");
        dictionary = dictionaryDao.selectOne(dictionary);
        Advertisements advertisements = new Advertisements();
        List<Advertisements> lists = this.advertisementsDao.selectAdverAllOpen();
        if (!lists.isEmpty()){
            Random random = new Random();
            int n = random.nextInt(lists.size());
            advertisements = lists.get(n);
            advertisements.setAdvertisementsImgUrl(dictionary.getDisc()+advertisements.getAdvertisementsImgUrl());
        }
        return ResultBean.getSuccess(advertisements);
    }



    public ResultBean selectAdverPollingTime() {
        Dictionary dictionary = new Dictionary();
        dictionary.setDictCode("pollingTime");
        List<Dictionary> list = dictionaryDao.selectDictByCode(dictionary);
        if (!list.isEmpty()){
            dictionary=list.get(0);
            return ResultBean.getSuccess(60000*Integer.parseInt(dictionary.getDisc()));
        }
        return ResultBean.getSuccess(list);
    }
}
